package com.example.binjiangapp.service.impl;

import com.example.binjiangapp.DAO.Dto.UserR;
import com.example.binjiangapp.DAO.User;
import com.example.binjiangapp.common.utils.encryptedPassword;
import com.example.binjiangapp.mapper.loginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class loginServiceImpl implements com.example.binjiangapp.service.loginService{
    @Autowired
    private loginMapper loginmaaper;

    /**
     * 是否有该用户并且账号密码是否正确
     * @param user
     * @return
     */
    @Override
    public Boolean hasUser(UserR user){
        Boolean res = false ;
        User user1=loginmaaper.getUser(user.getUsername());
        if(user1 == null){
            insertUser(user);
            res =  true;
        }else{
            res = encryptedPassword.decrypt(user1.getPassword()).equals(user.getPassword());
        }
        return  res;
    }

    @Override
    public void insertUser(UserR user){
        String enPass = encryptedPassword.encrypt(user.getPassword());
        System.out.println(user.getUsername());
        loginmaaper.insertUser(new User(user.getUsername(),enPass));
    }
}
